<?php

namespace common\library\traits;

use common\commands\Common;
use Yii;

trait APITrait
{
    public function getPage($default = 1)
    {
        if (Yii::$app->getRequest()->getIsPost()) {
            return Yii::$app->getRequest()->post('page', $default);
        }

        return Yii::$app->getRequest()->get('page', $default);
    }

    public function getPageSize($default = 10)
    {
        if (Yii::$app->getRequest()->getIsPost()) {
            return Yii::$app->getRequest()->post('page_size', $default);
        }

        return Yii::$app->getRequest()->get('page_size', $default);
    }

    public function getLangId($default = 1)
    {
        if (Yii::$app->getRequest()->getIsPost()) {
            return Yii::$app->getRequest()->post('lang_id', $default);
        }

        return Yii::$app->getRequest()->get('lang_id', $default);
    }

    public function response($data)
    {
        header('Content-Type: application/json; charset=' . Yii::$app->charset);
        exit(json_encode($data));
    }

    public function fail($code, $msg = null, $data = null)
    {
        $this->response(Common::getPackedResponse($code, $msg, $data));
    }

    public function failure($data = null, $msg = '系統繁忙')
    {
        $this->response(Common::getPackedResponse(ERROR, $msg, $data));
    }

    public function success($data = null)
    {
        $this->response(Common::getPackedResponse(SUCCESS, 'Success', $data));
    }

    public function invalidParam($msg = '参数错误')
    {
        $this->fail(MISSING_PARAMS, $msg);
    }

    public function noData($msg = null, $code = NO_DATA)
    {
        $this->response(Common::getPackedResponse($code, $msg));
    }

    /**
     * 變量必須爲數值或數值字符串，否則返回 invalid param 錯誤信息
     * @param $num
     */
    public function mustBeNumeric($num)
    {
        if (!is_numeric($num)) {
            $this->invalidParam();
        }
    }

    /**
     * $num1 必須大於 $num2，否則返回 invalid param 錯誤
     * @param $num1
     * @param $num2
     */
    public function mustGreaterThan($num1, $num2)
    {
        if ($num1 <= $num2) {
            $this->invalidParam();
        }
    }

    /**
     * $num1 必須小於 $num2，否則返回 invalid param 錯誤
     * @param $num1
     * @param $num2
     */
    public function mustLessThan($num1, $num2)
    {
        if ($num1 >= $num2) {
            $this->invalidParam();
        }
    }

    /**
     * 變量必須是非空的數組，否則返回 invalid param 錯誤信息
     * @param $num
     */
    public function mustBeArray($arr)
    {
        if (!is_array($arr) || count($arr) == 0) {
            $this->invalidParam();
        }
    }

    /**
     * 獲取請求參數，如果請求參數爲空，則返回 invalid param 錯誤信息
     * @param $name
     * @param string $method
     * @return array|mixed
     */
    public function get($name, $method = 'GET')
    {
        if ($method == 'GET') {
            $v = Yii::$app->getRequest()->get($name);
        } else {
            $v = Yii::$app->getRequest()->post($name);
        }

        if (!$v) {
            $this->invalidParam();
        }

        return $v;
    }

    public function post($name)
    {
        return $this->get($name, 'POST');
    }
}
